package com.happy.study.controller;

import com.happy.study.entity.*;
import com.happy.study.servier.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping ("/report")
public class ReportController {


    @Resource
    private IRecordService iRecordService;
    @Resource
    private IPaperService iPaperService;
    @Resource
    private IOptionService iOptionService;
    @Resource
    private ISubjectService iSubjectService;
    @Resource
    private IAnswerService iAnswerService;

    /**
     * 获取测试分析报告
     *
     * @param id: 测试记录id，根据此id查询试卷与选项以及所填答案
     * @return
     */
    @GetMapping ("{id}")
    public String report ( @PathVariable int id, Model model, HttpSession session ) {
        UserEntity userEntity = (UserEntity) session.getAttribute ( "userEntity" );
        // 1. 加载记录信息
        RecordEntity recordEntity = new RecordEntity ();
        recordEntity.setId ( id );
        recordEntity = iRecordService.querySingle ( recordEntity );
        // 2. 加载试卷信息
        PaperEntity paperEntity = iPaperService.querySingle ( recordEntity.getPaperId () );
        model.addAttribute ( "paperEntity", paperEntity );
        // 3. 加载试题列表信息
        SubjectEntity subjectEntity = new SubjectEntity ();
        subjectEntity.setPaperId ( paperEntity.getPaperId () );
        List<SubjectEntity> subjectEntities = iSubjectService.queryList ( subjectEntity );
        for ( SubjectEntity subject : subjectEntities ) {
            // 4. 加载选项信息
            OptionEntity optionEntity = new OptionEntity ();
            optionEntity.setSubjectId ( subject.getSubjectId () );
            List<OptionEntity> optionEntities = iOptionService.queryList ( optionEntity );
            subject.setOptionEntities ( optionEntities );
            // 5. 加载答案信息
            AnswerEntity answerEntity = new AnswerEntity ();
            answerEntity.setSubjectId ( subject.getSubjectId () );
            answerEntity.setUserId ( userEntity.getId () );
            answerEntity.setRecordId ( recordEntity.getId () );
            answerEntity = iAnswerService.querySingle ( answerEntity );
            subject.setAnswerEntity ( answerEntity );
        }
        model.addAttribute ( "subjectEntities", subjectEntities );
        return "report";
    }


}
